package ru.belokuriha_online.resort_hotels_belokuriha;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.app.AlertDialog;

import android.os.Bundle;
import android.os.Environment;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;


import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.Toast;

import android.view.*;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.database.sqlite.SQLiteDatabase;




public class Sravneniye extends Activity {
	public static File EDIR = Environment.getExternalStorageDirectory();	
	String filepricesPath1;	
	String fileaboutPath;
	String namesansniper;
	String id_namesansniper;
	String ssanatori_name, sssanatori_name, ssssanatori_name;
	String iid_name;
	String spprisepath;
	String spprisepathfull;
	Integer flags_srav;
	ArrayList<HashMap<String, String>> Datasniper;
	ArrayList<String> Datasniper1_1;
	ArrayList<String> Datasniper2_1;
	ArrayList<String> Datasniper1;
	ArrayList<String> Datasniper2;
	HashMap<String, String> sansnip;
	Spinner spin1;
	Spinner spin2;
	Integer i1,i2,k1,k2;
	String groupa1_1,groupa1_2;
	String groupa2_1,groupa2_2;
	ArrayList<HashMap<String, String>> groupData1,groupData2;
	ArrayList<HashMap<String, String>> childDataItem1,childDataItem2;
	ArrayList<ArrayList<HashMap<String, String>>> childData1,childData2;
	HashMap<String, String> m1,m2;
	int groupPositionsrav1,childPositionsrav1;
	ExpandableListView listsan1srav,listsan2srav;
	int saveposition;
	Integer saveor1,saveor2;
	Integer indexsnip1;
	String mmpp_1,mmpp_2,mmpp_3,mmpp_4,mmpp_5,mmpp_6;	
	Integer indexGX,indexGXs,indexIX,indexIXs;
	Integer gpos,childpos,gpos1,childpos1,gpos2,childpos2;
	SimpleExpandableListAdapter adapterpr2;
	int rotate1, rotate2;
	int rotatet1, rotatet2;
	int mstate1, mstate2 ;
	String f1, f2;
	String f11, f22;
	int FlagOnlain;
	PriceAdapter1 MyAdapterpr1;
	PriceAdapter2 MyAdapterpr2;
	View viewitem;
	private CharSequence[] spisokfav;
	AlertDialog.Builder buildergofav;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.sravneniye);
		try {
		flags_srav = 0;
		spprisepath = getIntent().getExtras().getString("spprisepath");
		spprisepathfull = getIntent().getExtras().getString("spprisepathfull");
		FlagOnlain = getIntent().getExtras().getInt("FlagOnlain");
		flags_srav = getIntent().getExtras().getInt("flags_srav");
		spisokfav = getResources().getTextArray(R.array.menufav);
		mstate1 = 0;
		mstate2 = 0;
		sssanatori_name = "";
		ssssanatori_name = "";
		gpos2 = null;
		childpos2 = null;
		if (flags_srav == 0) {
			gpos1 = null;
			childpos1 = null;		
		}		
		if (flags_srav == 1) {
			ssanatori_name = getIntent().getExtras().getString("san_name");
			sssanatori_name = ssanatori_name;
			iid_name = getIntent().getExtras().getString("id_name");
			mmpp_1 = getIntent().getExtras().getString("mmpp_1");
			mmpp_2 = getIntent().getExtras().getString("mmpp_2");
			mmpp_3 = getIntent().getExtras().getString("mmpp_3");
			mmpp_4 = getIntent().getExtras().getString("mmpp_4");
			mmpp_5 = getIntent().getExtras().getString("mmpp_5");
			mmpp_6 = getIntent().getExtras().getString("mmpp_6");
			
		}
		if (flags_srav == 2) {
			ssanatori_name = getIntent().getExtras().getString("san_name");
			sssanatori_name = ssanatori_name;
			iid_name = getIntent().getExtras().getString("id_name");
			gpos = getIntent().getExtras().getInt("gpos");
			childpos = getIntent().getExtras().getInt("childpos");
			gpos1 = gpos;
			childpos1 = childpos;
			
		}
		if (flags_srav == 3) {
			ssanatori_name = getIntent().getExtras().getString("san_name");
			sssanatori_name = ssanatori_name;
			iid_name = getIntent().getExtras().getString("id_name");
			gpos1 = null;
			childpos1 = null;
			
		}
		
		Datasniper =  new ArrayList<HashMap<String, String>>();
		Datasniper1_1 =  new ArrayList<String>();
		Datasniper2_1 =  new ArrayList<String>();
		Datasniper1 =  new ArrayList<String>();
		Datasniper2 =  new ArrayList<String>();
		saveor1 = 1;
		saveor2 = 1;

		try {       
			XmlPullParserFactory parserpull = XmlPullParserFactory.newInstance();
			parserpull.setValidating(false);
			XmlPullParser parser = parserpull.newPullParser();

			File xmlfilesanatories = new File(spprisepathfull);
			InputStream xmlfile = new FileInputStream(xmlfilesanatories);

			parser.setInput(xmlfile, null);

			while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) {

				if (parser.getEventType() == XmlPullParser.START_TAG
						&& parser.getName().equals("sanatori") 
						&& parser.getDepth() == 2) {
					sansnip = new HashMap<String, String>();


					namesansniper = parser.getAttributeValue(0);
					if (namesansniper == null) {
						namesansniper = "";
					}else {
						namesansniper = parser.getAttributeValue(0);	
					}

					id_namesansniper = parser.getAttributeValue(1);
					if (id_namesansniper == null) {
						id_namesansniper = "";
					}else {
						id_namesansniper = parser.getAttributeValue(1);	
					}

					sansnip.put("nssnip", namesansniper);
					sansnip.put("idnssnip", id_namesansniper);
					Datasniper.add(sansnip);
					Datasniper1.add(namesansniper);
					Datasniper2.add(namesansniper);
					Datasniper1_1.add(id_namesansniper);
					Datasniper2_1.add(id_namesansniper);
					if (flags_srav == 1) {
						if (namesansniper.equals(ssanatori_name)) {
							indexsnip1 = Datasniper1.size();
						}						
					}
					if (flags_srav == 2) {
						if (namesansniper.equals(ssanatori_name)) {
							indexsnip1 = Datasniper1.size();
						}						
					}
					if (flags_srav == 3) {
						if (namesansniper.equals(ssanatori_name)) {
							indexsnip1 = Datasniper1.size();
						}						
					}

				}	

				////*
				parser.next();
			}
			xmlfile.close();
			///\\\   

			////* 

			ArrayAdapter<String> adaptersnp1 = new ArrayAdapter<String>(getApplicationContext(), R.layout.mysimple_spinner_item, Datasniper1);
			ArrayAdapter<String> adaptersnp2 = new ArrayAdapter<String>(getApplicationContext(), R.layout.mysimple_spinner_item, Datasniper2);	
			adaptersnp1.setDropDownViewResource(R.layout.mysimple_spinner_dropdown_item);
			adaptersnp2.setDropDownViewResource(R.layout.mysimple_spinner_dropdown_item);
			Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
			Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
			spin1.setAdapter(adaptersnp1);
			spin2.setAdapter(adaptersnp2);
			if (flags_srav == 1) {
				if (indexsnip1 > 0) 	{
					spin1.setSelection(indexsnip1 -1);
				}
			}
			if (flags_srav == 2) {
				if (indexsnip1 > 0) 	{
					spin1.setSelection(indexsnip1 -1);
				}
			}
			if (flags_srav == 3) {
				if (indexsnip1 > 0) 	{
					spin1.setSelection(indexsnip1 -1);
				}
			}
			listsan1srav = (ExpandableListView) findViewById(R.id.expandableListViewSan1);
			listsan2srav = (ExpandableListView) findViewById(R.id.expandableListViewSan2);


			spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View v,
						int position, long id) {

					f1 = Datasniper.get(position).get("idnssnip").toString();
					f11 = Datasniper.get(position).get("nssnip").toString();
					///\
					
					
					
					
					try {

						XmlPullParserFactory parserxmlpull1 = XmlPullParserFactory.newInstance();
						parserxmlpull1.setValidating(false);
						XmlPullParser parserxml1 = parserxmlpull1.newPullParser();

						File xmlfileprices1 = new File(spprisepathfull);
						InputStream xmlfiles1 = new FileInputStream(xmlfileprices1);

						parserxml1.setInput(xmlfiles1, null);

						groupa1_1 = "";
						i1 = 0;
						k1 = 0;

						groupData1 = new ArrayList<HashMap<String, String>>();

						while (parserxml1.getEventType()!= XmlPullParser.END_DOCUMENT) {

							if (parserxml1.getEventType() == XmlPullParser.START_TAG
									&& parserxml1.getName().equals(f1) 
									&& parserxml1.getDepth() == 3) {
								/////

								groupa2_1 = parserxml1.getAttributeValue(0);
								if (!groupa1_1.equals(groupa2_1)) {


									m1 = new HashMap<String, String>();
									m1.put("groupName1", parserxml1.getAttributeValue(0)); 
									groupData1.add(m1); 
									groupa1_1 = parserxml1.getAttributeValue(0);
									k1 = 1; 
									if (i1==0){

										childData1 = new ArrayList<ArrayList<HashMap<String, String>>>(); 
										i1=1;
									}

									childDataItem1 = new ArrayList<HashMap<String, String>>();

								}
								//


								m1 = new HashMap<String, String>();

								String sr1_1 = parserxml1.getAttributeValue(0);
								if (sr1_1 == null) {
									sr1_1 = "";
								}else {
									sr1_1 = parserxml1.getAttributeValue(0);	
								}
								String sr1_2 = parserxml1.getAttributeValue(1);
								if (sr1_2 == null) {
									sr1_2 = "";
								}else {
									sr1_2 = parserxml1.getAttributeValue(1);	
								}
								String sr1_3 = parserxml1.getAttributeValue(2);
								if (sr1_3 == null) {
									sr1_3 = "";
								}else {
									sr1_3 = parserxml1.getAttributeValue(2);	
								}
								String sr1_4 = parserxml1.getAttributeValue(3);
								if (sr1_4 == null) {
									sr1_4 = "";
								}else {
									sr1_4 = parserxml1.getAttributeValue(3);	
								}
								String sr1_5 = parserxml1.getAttributeValue(4);
								if (sr1_5 == null) {
									sr1_5 = "";
								}else {
									sr1_5 = parserxml1.getAttributeValue(4);	
								}
								String sr1_6 = parserxml1.getAttributeValue(5);
								if (sr1_6 == null) {
									sr1_6 = "";
								}else {
									sr1_6 = parserxml1.getAttributeValue(5);	
								}

								m1.put("sr1_1", sr1_1);
								m1.put("sr1_2", sr1_2);                    
								m1.put("sr1_3", sr1_3);
								m1.put("sr1_4", sr1_4);
								m1.put("sr1_5", sr1_5);
								m1.put("sr1_6", sr1_6);
								m1.put("sr1_7", parserxml1.nextText());

								childDataItem1.add(m1);
								if (flags_srav == 1) {
								if (sr1_1.equals(mmpp_1)) {
									if (sr1_2.equals(mmpp_2)) {
										if (sr1_3.equals(mmpp_3)) {
											if (sr1_4.equals(mmpp_4)) {
												if (sr1_5.equals(mmpp_5)) {
													if (sr1_6.equals(mmpp_6)) {
														indexGX = groupData1.size();
														if (indexGX == 0) {
															indexGXs = null;
															gpos1 = indexGXs;
														} else {
															indexGXs = indexGX-1;
															gpos1 = indexGXs;
														}
														indexIX = childDataItem1.size();
														if (indexIX == 0) {
															indexIXs = null;
															childpos1 = indexIXs;
														} else {
															indexIXs = indexIX-1;
															childpos1 = indexIXs;
														}											
													}
												}
											}
										}
									}
								}
								}
								if (k1==1) {                     	 
									childData1.add(childDataItem1);
									k1=0;
								}

								// 
							}


							parserxml1.next();

						}


						xmlfiles1.close(); 

						String groupFrom[] = new String[] {"groupName1"};

						int groupTo[] = new int[] {R.id.textg1};


						String childFrom[] = new String[] {"sr1_1","sr1_2","sr1_3","sr1_4","sr1_5","sr1_6","sr1_7"};

						int childTo[] = new int[] {R.id.textView_category,R.id.textView_area_n,R.id.textView_razm,R.id.textView_time,R.id.textView_food,R.id.textView_price,R.id.textView_prim};
						MyAdapterpr1 = new PriceAdapter1(getApplicationContext(), groupData1, childData1, f11);
						MyAdapterpr1.notifyDataSetChanged();
						
						SimpleExpandableListAdapter adapterpr1 = new SimpleExpandableListAdapter(
								getApplicationContext(),
								groupData1,
								R.layout.simple_expandable_list_item_1_my,
								groupFrom,
								groupTo,
								childData1,
								R.layout.items_list2,
								childFrom,
								childTo);
								
						

						//////




						//adapterpr1.notifyDataSetChanged();
						//listsan1srav.setAdapter(adapterpr1);
						if (groupData1.size() > 0) { 
							listsan1srav.setAdapter(MyAdapterpr1);
						} else
						{
							adapterpr1.notifyDataSetChanged();
							listsan1srav.setAdapter(adapterpr1);
						}
						///\\

						if (rotatet1 == rotate1) {
							if (mstate1 == 0) {
								if (!f11.equals(sssanatori_name)) {	
							gpos1 = null;
							childpos1 = null;
								}
							}
							mstate1 = 0;
						} 
						else {
						}
						
						rotate1 = rotatet1;
						///\\
						
								try {
								listsan1srav.expandGroup(gpos1);
								listsan1srav.setSelectedChild(gpos1, childpos1, true);
								}
								catch (Throwable t) {
									
								}
					
				
						listsan1srav.setOnChildClickListener(new OnChildClickListener() { 
							@Override
							public boolean onChildClick(ExpandableListView parent, View v,
									int groupPosition, int childPosition, long id) {
							try {
								sssanatori_name = f11;
								gpos1 = groupPosition;
								childpos1 = childPosition;
								gpos = groupPosition;
								childpos = childPosition;
								indexGXs = groupPosition;
								indexIXs = childPosition;
								GoFavor1(f11);
								MyAdapterpr1.notifyDataSetChanged();
							}
							catch (Throwable t) {
								
							}
								return false;					
							} 
						});
						
								
						
					}
					catch (Throwable t) {
					//	MyAdapterpr1 = null;
					}
					///\


				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					mstate1 = 0;

				} 
			});



			spin2.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View v,
						int position, long id) {

					f2 = Datasniper.get(position).get("idnssnip").toString();
					f22 = Datasniper.get(position).get("nssnip").toString();
					
					///\
					
					if (rotatet2 == rotate2) {
						if (mstate2 == 0) {
							if (!f22.equals(ssssanatori_name)) {
						gpos2 = null;
						childpos2 = null;
							}
						}
						mstate2 = 0;
					} 
					else {
					}
					rotate2 = rotatet2;
					
					try {

						XmlPullParserFactory parserxmlpull2 = XmlPullParserFactory.newInstance();
						parserxmlpull2.setValidating(false);
						XmlPullParser parserxml2 = parserxmlpull2.newPullParser();

						File xmlfileprices2 = new File(spprisepathfull);
						InputStream xmlfiles2 = new FileInputStream(xmlfileprices2);

						parserxml2.setInput(xmlfiles2, null);

						groupa1_2 = "";
						i2 = 0;
						k2 = 0;

						groupData2 = new ArrayList<HashMap<String, String>>();

						while (parserxml2.getEventType()!= XmlPullParser.END_DOCUMENT) {

							if (parserxml2.getEventType() == XmlPullParser.START_TAG
									&& parserxml2.getName().equals(f2) 
									&& parserxml2.getDepth() == 3) {
								/////

								groupa2_2 = parserxml2.getAttributeValue(0);
								if (!groupa1_2.equals(groupa2_2)) {


									m2 = new HashMap<String, String>();
									m2.put("groupName2", parserxml2.getAttributeValue(0)); 
									groupData2.add(m2); 
									groupa1_2 = parserxml2.getAttributeValue(0);
									k2 = 1; 
									if (i2==0){

										childData2 = new ArrayList<ArrayList<HashMap<String, String>>>(); 
										i2=1;
									}

									childDataItem2 = new ArrayList<HashMap<String, String>>();

								}
								//


								m2 = new HashMap<String, String>();

								String sr2_1 = parserxml2.getAttributeValue(0);
								if (sr2_1 == null) {
									sr2_1 = "";
								}else {
									sr2_1 = parserxml2.getAttributeValue(0);	
								}
								String sr2_2 = parserxml2.getAttributeValue(1);
								if (sr2_2 == null) {
									sr2_2 = "";
								}else {
									sr2_2 = parserxml2.getAttributeValue(1);	
								}
								String sr2_3 = parserxml2.getAttributeValue(2);
								if (sr2_3 == null) {
									sr2_3 = "";
								}else {
									sr2_3 = parserxml2.getAttributeValue(2);	
								}
								String sr2_4 = parserxml2.getAttributeValue(3);
								if (sr2_4 == null) {
									sr2_4 = "";
								}else {
									sr2_4 = parserxml2.getAttributeValue(3);	
								}
								String sr2_5 = parserxml2.getAttributeValue(4);
								if (sr2_5 == null) {
									sr2_5 = "";
								}else {
									sr2_5 = parserxml2.getAttributeValue(4);	
								}
								String sr2_6 = parserxml2.getAttributeValue(5);
								if (sr2_6 == null) {
									sr2_6 = "";
								}else {
									sr2_6 = parserxml2.getAttributeValue(5);	
								}

								m2.put("sr2_1", sr2_1);
								m2.put("sr2_2", sr2_2);                    
								m2.put("sr2_3", sr2_3);
								m2.put("sr2_4", sr2_4);
								m2.put("sr2_5", sr2_5);
								m2.put("sr2_6", sr2_6);
								m2.put("sr2_7", parserxml2.nextText());

								childDataItem2.add(m2);
								if (k2==1) {                     	 
									childData2.add(childDataItem2);
									k2=0;
								}

								// 
							}


							parserxml2.next();

						}


						xmlfiles2.close(); 

						String groupFrom[] = new String[] {"groupName2"};

						int groupTo[] = new int[] {R.id.textg1};


						String childFrom[] = new String[] {"sr2_1","sr2_2","sr2_3","sr2_4","sr2_5","sr2_6","sr2_7"};

						int childTo[] = new int[] {R.id.textView_category,R.id.textView_area_n,R.id.textView_razm,R.id.textView_time,R.id.textView_food,R.id.textView_price,R.id.textView_prim};

						adapterpr2 = new SimpleExpandableListAdapter(
								getApplicationContext(),
								groupData2,
								R.layout.simple_expandable_list_item_1_my,
								groupFrom,
								groupTo,
								childData2,
								R.layout.items_list2,
								childFrom,
								childTo);


						//////
						MyAdapterpr2 = new PriceAdapter2(getApplicationContext(), groupData2, childData2, f22);
						MyAdapterpr2.notifyDataSetChanged();

						listsan2srav = (ExpandableListView) findViewById(R.id.expandableListViewSan2);

						//adapterpr2.notifyDataSetChanged();
						//listsan2srav.setAdapter(adapterpr2);
						MyAdapterpr2.notifyDataSetChanged();
						if (groupData2.size() > 0) {
							listsan2srav.setAdapter(MyAdapterpr2);
						} else {								
							adapterpr2.notifyDataSetChanged();
							listsan2srav.setAdapter(adapterpr2);
						}
						//if ((gpos2 != null) & (childpos2 != null)) {
							try {
							listsan2srav.expandGroup(gpos2);
							listsan2srav.setSelectedChild(gpos2, childpos2, true);
							}
							catch (Throwable t) {

							}
						//}
						listsan2srav.setOnChildClickListener(new OnChildClickListener() { 
							@Override
							public boolean onChildClick(ExpandableListView parent, View v,
									int groupPosition, int childPosition, long id) {
								ssssanatori_name = f22;
								gpos2 = groupPosition;
								childpos2 = childPosition;
								GoFavor2(f22);
								MyAdapterpr2.notifyDataSetChanged();
								return false;								
													
							} 
						});
					

					}
					catch (Throwable t) {
						
						
					}
					///\


				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					mstate2 = 0;
					
					
				} 
			});


			////*


		}
		catch (Throwable terror) {

		}
		////////////////





		final TextView exitsrav =(TextView) findViewById(R.id.textViewClosesrav);
		exitsrav.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setResult(2);
				finish();	
			}	
		});
	} catch (Exception e) {
		// TODO: handle exception
	}
	}
	private void GoFavor1(final String f1_2) {
		///
		final ArrayList<HashMap<String, String>>  chData = childData1.get(gpos1);
		final String pfavor_category = chData.get(childpos1).get("sr1_1").toString();
		final String pfavor_area_n = chData.get(childpos1).get("sr1_2").toString();
		final String pfavor_razm = chData.get(childpos1).get("sr1_3").toString();
		final String pfavor_time = chData.get(childpos1).get("sr1_4").toString();
		final String pfavor_food = chData.get(childpos1).get("sr1_5").toString();
		final String pfavor_price = chData.get(childpos1).get("sr1_6").toString();
		final String pfavor_prim = chData.get(childpos1).get("sr1_7").toString();
		final PriceAdapter1 MyAdapterpr_ = new PriceAdapter1(getApplicationContext(), groupData1, childData1, f1_2);
		final String description_calendar1 = f1_2+" / " + chData.get(childpos1).get("sr1_1").toString()+ " / " +
				chData.get(childpos1).get("sr1_2").toString()+" / " + chData.get(childpos1).get("sr1_3").toString()+" / " + chData.get(childpos1).get("sr1_4").toString()+" / " +
				chData.get(childpos1).get("sr1_5").toString()+" (" +chData.get(childpos1).get("sr1_7").toString() + ") "+
				" ....... " + chData.get(childpos1).get("sr1_6").toString() +" "+getString(R.string.hotel_prise);
		buildergofav = new AlertDialog.Builder(this);
		buildergofav.setTitle("");		
		buildergofav.setItems(spisokfav, 
			new DialogInterface.OnClickListener() {
							
			@Override
			public void onClick(DialogInterface dialog, int item) {
				switch (item) { 

				case 0:
						
					try {
						
						//PriceAdapter1 MyAdapterpr_ = new PriceAdapter1(getApplicationContext(), groupData1, childData1, f1_2);
						
						if (MyAdapterpr_.countf(gpos1, childpos1, f1_2) > 0) {
							Toast.makeText(getApplicationContext(),getString(R.string.f_add_no).toString(),Toast.LENGTH_LONG ).show();
						} else {
							//insert item in favorits
							Favorsql sqlinsert = new Favorsql(getApplicationContext());
					     	// ���� ������ � ������
							SQLiteDatabase sqldb_insert = sqlinsert.getWritableDatabase();
							String sqlinserttext = "INSERT INTO " + Favorsql.dtable + 
								     " (" + Favorsql.DFAVORSAN + "," + Favorsql.DFAVORCATEGORY + "," + Favorsql.DFAVORAREAN +
								     "," + Favorsql.DFAVORRAZM + "," + Favorsql.DFAVORTIME + "," + Favorsql.DFAVORFOOD + "," + Favorsql.DFAVORPRICE + "," + Favorsql.DFAVORPRIM +
								     ") VALUES ('" + f1_2 + "','" + pfavor_category + "','" + pfavor_area_n + 
								     "','" + pfavor_razm + "','" + pfavor_time + "','" + pfavor_food + "','" + pfavor_price + "','" + pfavor_prim +
								     "')";
							
							sqldb_insert.execSQL(sqlinserttext);
							MyAdapterpr_.viewfavorit(viewitem,gpos1, childpos1);
							sqldb_insert.close();
							sqlinsert.close();
							MyAdapterpr_.notifyDataSetChanged();
							MyAdapterpr1.notifyDataSetChanged();
							Toast.makeText(getApplicationContext(),getString(R.string.f_add).toString(),Toast.LENGTH_LONG ).show();
						}					
						
					} catch (Exception e) {
						
					}
				break;
				
				case 1:
					
					try {
						///
						Favorsql sqldbcdel_el  = new Favorsql(getApplicationContext());
						SQLiteDatabase sqldbdel_el = sqldbcdel_el.getWritableDatabase();
						String delEl = "DELETE FROM " + Favorsql.dtable + 
								" 	WHERE ("+ Favorsql.DFAVORSAN + " = '" + f1_2 + "') AND (" 
									 + Favorsql.DFAVORCATEGORY + " = '" + pfavor_category + "') AND ("
									 + Favorsql.DFAVORAREAN + " = '" + pfavor_area_n + "') AND ("
									 + Favorsql.DFAVORRAZM + " = '" + pfavor_razm + "') AND ("
									 + Favorsql.DFAVORTIME + " = '" + pfavor_time + "') AND ("
									 + Favorsql.DFAVORFOOD + " = '" + pfavor_food + "') AND ("
									 + Favorsql.DFAVORPRICE +  " = '" + pfavor_price + "')" +
									 " ";
								
								sqldbdel_el.execSQL(delEl);
								sqldbdel_el.close();
								sqldbcdel_el.close();
						if (MyAdapterpr_.countf(gpos1, childpos1, f1_2) > 0) {
							Toast.makeText(getApplicationContext(),getString(R.string.f_del).toString(),Toast.LENGTH_LONG ).show();
						}	
						MyAdapterpr_.notifyDataSetChanged();
						MyAdapterpr1.notifyDataSetChanged();
						
					} catch (Exception e) {
						
					}
				break;
				
				case 2:
					try { 
						
						Intent calendarIntent1 = new Intent(Intent.ACTION_INSERT);
						calendarIntent1.setType("vnd.android.cursor.item/event"); 
						calendarIntent1.putExtra(Events.TITLE, getString(R.string.calend_title));
						calendarIntent1.putExtra(Events.EVENT_LOCATION, f1_2 + " "+ getString(R.string.location));
						calendarIntent1.putExtra(Events.DESCRIPTION, description_calendar1);					                 
		                calendarIntent1.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
		                calendarIntent1.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,System.currentTimeMillis()); 
		                calendarIntent1.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,System.currentTimeMillis()); 
		                startActivity(calendarIntent1);				
					} catch (Exception e) {

					}
				break;
				case 3:
					try {			
						//
						Intent sendIntent = new Intent();
						sendIntent.setAction(Intent.ACTION_SEND);
						sendIntent.putExtra(Intent.EXTRA_TEXT, description_calendar1);
						sendIntent.setType("text/plain");
						startActivity(sendIntent);
						//
					} catch (Exception e) {

					}
				break;
				}
			}
		}				
				).create().show();
		
		///
		

	}
	private void GoFavor2(final String f1_2) {
		////
		final ArrayList<HashMap<String, String>>  chData2 = childData2.get(gpos2);
		final String pfavor_category = chData2.get(childpos2).get("sr2_1").toString();
		final String pfavor_area_n = chData2.get(childpos2).get("sr2_2").toString();
		final String pfavor_razm = chData2.get(childpos2).get("sr2_3").toString();
		final String pfavor_time = chData2.get(childpos2).get("sr2_4").toString();
		final String pfavor_food = chData2.get(childpos2).get("sr2_5").toString();
		final String pfavor_price = chData2.get(childpos2).get("sr2_6").toString();
		final String pfavor_prim = chData2.get(childpos2).get("sr2_7").toString();
		final PriceAdapter2 MyAdapterpr_ = new PriceAdapter2(getApplicationContext(), groupData2, childData2, f1_2);
		final String description_calendar2 = f1_2+" / " + chData2.get(childpos2).get("sr2_1").toString()+ " / " +
				chData2.get(childpos2).get("sr2_2").toString()+" / " + chData2.get(childpos2).get("sr2_3").toString()+" / " + chData2.get(childpos2).get("sr2_4").toString()+" / " +
				chData2.get(childpos2).get("sr2_5").toString()+" (" +chData2.get(childpos2).get("sr2_7").toString() + ") "+
				" ....... " + chData2.get(childpos2).get("sr2_6").toString() +" "+getString(R.string.hotel_prise);
		buildergofav = new AlertDialog.Builder(this);
		buildergofav.setTitle("");		
		buildergofav.setItems(spisokfav, 
			new DialogInterface.OnClickListener() {
							
			@Override
			public void onClick(DialogInterface dialog, int item) {
				switch (item) { 

				case 0:
						
					try {
						
						//PriceAdapter1 MyAdapterpr_ = new PriceAdapter1(getApplicationContext(), groupData1, childData1, f1_2);
						
						if (MyAdapterpr_.countf(gpos2, childpos2, f1_2) > 0) {
							Toast.makeText(getApplicationContext(),getString(R.string.f_add_no).toString(),Toast.LENGTH_LONG ).show();
						} else {
							//insert item in favorits
							Favorsql sqlinsert = new Favorsql(getApplicationContext());
					     	// ���� ������ � ������
							SQLiteDatabase sqldb_insert = sqlinsert.getWritableDatabase();
							String sqlinserttext = "INSERT INTO " + Favorsql.dtable + 
								     " (" + Favorsql.DFAVORSAN + "," + Favorsql.DFAVORCATEGORY + "," + Favorsql.DFAVORAREAN +
								     "," + Favorsql.DFAVORRAZM + "," + Favorsql.DFAVORTIME + "," + Favorsql.DFAVORFOOD + "," + Favorsql.DFAVORPRICE + "," + Favorsql.DFAVORPRIM +
								     ") VALUES ('" + f1_2 + "','" + pfavor_category + "','" + pfavor_area_n + 
								     "','" + pfavor_razm + "','" + pfavor_time + "','" + pfavor_food + "','" + pfavor_price + "','" + pfavor_prim +
								     "')";
							
							sqldb_insert.execSQL(sqlinserttext);
							MyAdapterpr_.viewfavorit(viewitem,gpos2, childpos2);
							sqldb_insert.close();
							sqlinsert.close();
							MyAdapterpr_.notifyDataSetChanged();
							MyAdapterpr2.notifyDataSetChanged();
							Toast.makeText(getApplicationContext(),getString(R.string.f_add).toString(),Toast.LENGTH_LONG ).show();
						}					
						
					} catch (Exception e) {
						
					}
				break;
				
				case 1:
					
					try {
						///
						Favorsql sqldbcdel_el  = new Favorsql(getApplicationContext());
						SQLiteDatabase sqldbdel_el = sqldbcdel_el.getWritableDatabase();
						String delEl = "DELETE FROM " + Favorsql.dtable + 
								" 	WHERE ("+ Favorsql.DFAVORSAN + " = '" + f1_2 + "') AND (" 
									 + Favorsql.DFAVORCATEGORY + " = '" + pfavor_category + "') AND ("
									 + Favorsql.DFAVORAREAN + " = '" + pfavor_area_n + "') AND ("
									 + Favorsql.DFAVORRAZM + " = '" + pfavor_razm + "') AND ("
									 + Favorsql.DFAVORTIME + " = '" + pfavor_time + "') AND ("
									 + Favorsql.DFAVORFOOD + " = '" + pfavor_food + "') AND ("
									 + Favorsql.DFAVORPRICE +  " = '" + pfavor_price + "')" +
									 " ";
								
								sqldbdel_el.execSQL(delEl);
								sqldbdel_el.close();
								sqldbcdel_el.close();
						if (MyAdapterpr_.countf(gpos2, childpos2, f1_2) > 0) {
							Toast.makeText(getApplicationContext(),getString(R.string.f_del).toString(),Toast.LENGTH_LONG ).show();
						}	
						MyAdapterpr_.notifyDataSetChanged();
						MyAdapterpr2.notifyDataSetChanged();
						
					} catch (Exception e) {
						
					}
				break;
				
				case 2:
					try { 
						
						Intent calendarIntent2 = new Intent(Intent.ACTION_INSERT);
						calendarIntent2.setType("vnd.android.cursor.item/event"); 
						calendarIntent2.putExtra(Events.TITLE, getString(R.string.calend_title));
						calendarIntent2.putExtra(Events.EVENT_LOCATION, f1_2 + " "+ getString(R.string.location));
						calendarIntent2.putExtra(Events.DESCRIPTION, description_calendar2);					                 
		                calendarIntent2.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
		                calendarIntent2.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,System.currentTimeMillis()); 
		                calendarIntent2.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,System.currentTimeMillis()); 
		                startActivity(calendarIntent2);				
					} catch (Exception e) {

					}
				break;
				case 3:
					try {			
						//
						Intent sendIntent = new Intent();
						sendIntent.setAction(Intent.ACTION_SEND);
						sendIntent.putExtra(Intent.EXTRA_TEXT, description_calendar2);
						sendIntent.setType("text/plain");
						startActivity(sendIntent);
						//
					} catch (Exception e) {

					}
				break;
				}
			}
		}				
				).create().show();
		
		
		
		////
		
	}

	protected void Resume() { 
		super.onResume();		
		
		
			
				
			
	}




	public boolean onCreateOptionsMenu(Menu menu) { 

		getMenuInflater().inflate(R.menu.pmenu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) { 

		menu.findItem(R.id.loaditem1).setEnabled(true);
		menu.findItem(R.id.pitem2).setEnabled(true);
		menu.findItem(R.id.abitem3).setEnabled(true);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) { 

		case R.id.loaditem1: 
			GoMain();
			break; 


		case R.id.pitem2:
			//
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				startActivityForResult(Preferintent,0);
			} catch (Exception e) {

			}
			break; 
		case R.id.abitem3: 
			//
			try {
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				startActivityForResult(intentapp,0);
			} catch (Exception e) {

			}
			break;
		}       


		return super.onOptionsItemSelected(item); 

	}
	private void GoMain() {
		AlertDialog.Builder buildergomain = new AlertDialog.Builder(this);
		buildergomain.setTitle(R.string.lt_app);		
		buildergomain.setMessage(R.string.lmmm_app);		
		buildergomain.setNegativeButton(R.string.qn_app, null);
		buildergomain.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				setResult(7);
				finish(); 	            	

			}
		}).create().show();	
	}
	protected void onRestoreInstanceState(Bundle savedInstanceState) { 

		super.onRestoreInstanceState(savedInstanceState);
		  rotate1 = savedInstanceState.getInt("rotate1");
		  rotate2 = savedInstanceState.getInt("rotate2");
		  sssanatori_name = savedInstanceState.getString("sssanatori_name");
		  ssssanatori_name = savedInstanceState.getString("ssssanatori_name");
		  //
		 	try {			 	
		 		gpos1 = savedInstanceState.getInt("gpos1");
			 	childpos1 = savedInstanceState.getInt("childpos1");
		 	}
			catch (Throwable t) {

			}
		 	try {
			gpos2 = savedInstanceState.getInt("gpos2");
			childpos2 = savedInstanceState.getInt("childpos2");
		 	}
			catch (Throwable t) {

			}
		
		 	rotatet1 = getResources().getConfiguration().orientation;
			mstate1 = 0;
			
			rotatet2 = getResources().getConfiguration().orientation;
			mstate2 = 0;
			
			
			
			

	} 

	protected void onSaveInstanceState(Bundle outState) { 
		super.onSaveInstanceState(outState);
		outState.putInt("rotate1", rotate1);
		outState.putInt("rotate2", rotate2);
		outState.putString("sssanatori_name", sssanatori_name);
		outState.putString("ssssanatori_name", ssssanatori_name);
		
			try {
				
			outState.putInt("gpos1", gpos1);
			outState.putInt("childpos1", childpos1);
			}
			catch (Throwable t) {

			}
		
			try {
			outState.putInt("gpos2", gpos2);
			outState.putInt("childpos2", childpos2);
			}
			catch (Throwable t) {

			}
		
	} 
	 protected void onStart() { 
	      super.onStart(); 
	     }
	 
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 7) {
			setResult(7);
			finish();
		}
		//
		if (resultCode ==  5) {
			try {
				openOptionsMenu();
			} catch (Exception e) {

			}
		}
		if (resultCode ==  9) {
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				startActivityForResult(Preferintent,0);
			} catch (Exception e) {

			}
		}
		if (resultCode ==  10) {
			try{
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				startActivityForResult(intentapp,0);	
			} catch (Exception e) {

			}
		}

	}


}
